﻿package plugin.accomplishment.handler
{
    import com.core.*;
    import com.sh.game.util.*;
    import flash.utils.*;
    import manager.*;
    import plugin.accomplishment.consts.*;
    import pluginfw.nio.*;
    import pluginfw.nio.imsg.*;
    import view.*;

    public class AccompHandler extends Object implements IGroupHandler
    {
        private var isFirst:Boolean = true;

        public function AccompHandler()
        {
            return;
        }// end function

        public function dealSimpleMessage(param1:ISimpleMessage) : void
        {
            var _loc_2:Object = null;
            var _loc_3:Object = null;
            var _loc_4:Object = null;
            var _loc_5:Object = null;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            var _loc_9:Object = null;
            var _loc_10:Object = null;
            var _loc_11:Array = null;
            var _loc_12:int = 0;
            switch(param1.getCmd())
            {
                case CMDAchievement.GetAchievement:
                {
                    _loc_2 = param1.getParam();
                    ModelProxy.change("data", "achievement", _loc_2[0]);
                    if (_loc_2[1] == 1)
                    {
                        ModelProxy.change("plugin", "change", {target:"achievement", point:UIGlobel.pointsDic["cj"]});
                    }
                    ModelProxy.change("achievement", "update", null);
                    ModelProxy.change("mainui", "updateAchievement", null);
                    if (this.isFirst)
                    {
                        for each (_loc_5 in _loc_2[0])
                        {
                            
                            if (_loc_5 is Array)
                            {
                                if (_loc_5[1] == 1)
                                {
                                    ModelProxy.change("mainui", "playcj", null);
                                    break;
                                }
                            }
                        }
                        this.isFirst = false;
                    }
                    break;
                }
                case CMDAchievement.GetRewards:
                {
                    _loc_2 = param1.getParam();
                    _loc_3 = ModelProxy.getValue("data", "achievement");
                    if (_loc_3)
                    {
                        _loc_3["a" + _loc_2[0]] = [_loc_2[0], 2];
                        _loc_6 = _loc_3["res"];
                        _loc_7 = _loc_2[1];
                        if (_loc_6 != _loc_7)
                        {
                            _loc_8 = _loc_7 - _loc_6;
                            if (_loc_8 > 0)
                            {
                                WarningManager.show(16, ["成就积分", "+" + _loc_8, _loc_7]);
                            }
                            else if (_loc_8 < 0)
                            {
                                WarningManager.show(16, ["成就积分", _loc_8, _loc_7]);
                            }
                        }
                        _loc_3["res"] = _loc_7;
                        ModelProxy.change("achievement", "update", null);
                    }
                    if (_loc_2[2] > 0)
                    {
                        _loc_9 = ModelProxy.getValue("data", "titles");
                        DialogMgr.instance.open(AppDialog.ADD_TITLE, _loc_2[2]);
                        if (_loc_9)
                        {
                            _loc_9.source.push(_loc_2[2]);
                            ModelProxy.change("role", "updatetitles", null);
                        }
                    }
                    ModelProxy.change("mainui", "updateAchievement", null);
                    break;
                }
                case CMDAchievement.AchievementComplete:
                {
                    _loc_2 = param1.getParam();
                    _loc_3 = ModelProxy.getValue("data", "achievement");
                    if (_loc_3)
                    {
                        _loc_3["a" + _loc_2] = [_loc_2, 1];
                        _loc_10 = ConfigDictionary.data.achievement;
                        if (_loc_10[_loc_2])
                        {
                            ModelProxy.change("movetips", "error", "完成成就：" + _loc_10[_loc_2].name);
                        }
                        ModelProxy.change("achievement", "update", null);
                        ModelProxy.change("mainui", "updateAchievement", null);
                    }
                    else
                    {
                        ModelProxy.change("plugin", "sendSysMsg", {name:"achievement", param:0, cmd:CMDAchievement.GetAchievement});
                    }
                    ModelProxy.change("mainui", "playcj", null);
                    break;
                }
                case CMDAchievement.AcmDH:
                {
                    _loc_2 = param1.getParam();
                    _loc_3 = ModelProxy.getValue("data", "achievement");
                    if (_loc_3)
                    {
                        _loc_3.xz = _loc_2[0];
                        _loc_3.res = _loc_2[1];
                        ModelProxy.change("achievement", "update", null);
                    }
                    break;
                }
                case CMDAchievement.GetAllRewards:
                {
                    _loc_2 = param1.getParam();
                    _loc_4 = ModelProxy.getValue("data", "titles");
                    if (_loc_2[1])
                    {
                        _loc_11 = _loc_2[1].source;
                        if (_loc_11.length > 0)
                        {
                            DialogMgr.instance.open(AppDialog.ADD_TITLE, _loc_11[0]);
                        }
                    }
                    if (_loc_4)
                    {
                        if (_loc_2[1])
                        {
                            for each (_loc_12 in _loc_2[1].source)
                            {
                                
                                _loc_4.source.push(_loc_12);
                            }
                        }
                    }
                    ModelProxy.change("data", "achievement", _loc_2[0]);
                    ModelProxy.change("role", "updatetitles", null);
                    ModelProxy.change("achievement", "update", null);
                    ModelProxy.change("mainui", "updateAchievement", null);
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        public function dealBufferMessage(param1:IBufferMessage) : void
        {
            var _loc_2:Object = null;
            var _loc_3:Input = null;
            var _loc_4:ByteArray = null;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            switch(param1.getCmd())
            {
                case CMDAchievement.GetXunZdh:
                {
                    _loc_2 = new Object();
                    _loc_3 = new Input(null);
                    _loc_4 = param1.getBody();
                    _loc_4.position = 0;
                    _loc_3.setBytes(_loc_4);
                    _loc_2.acmScore = _loc_3.readVarInt(false);
                    _loc_5 = _loc_3.readShort();
                    _loc_2.medals = new Array();
                    _loc_6 = 0;
                    while (_loc_6 < _loc_5)
                    {
                        
                        _loc_2.medals[_loc_6] = _loc_3.readVarInt(false);
                        _loc_6++;
                    }
                    ModelProxy.change("achievement", "GetXunZdh", _loc_2);
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        public function dealProxyMessage(param1:IProxyMessage) : void
        {
            return;
        }// end function

    }
}
